package com.empire.module.member.service.hotelrecord;

import com.alibaba.fastjson.JSONObject;
import com.empire.module.member.dal.dataobject.user.MemberUserDO;
import com.empire.module.member.service.datahub.vo.HotelRecordDataHubVO;
import com.empire.module.member.service.user.MemberUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import com.empire.module.member.controller.admin.hotelrecord.vo.*;
import com.empire.module.member.dal.dataobject.hotelrecord.HotelRecordDO;
import com.empire.framework.common.pojo.PageResult;
import com.empire.framework.common.util.object.BeanUtils;

import com.empire.module.member.dal.mysql.hotelrecord.HotelRecordMapper;

import javax.annotation.Resource;

import java.time.LocalDateTime;

import static com.empire.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.empire.module.member.enums.ErrorCodeConstants.*;

/**
 * 旅馆住宿 Service 实现类
 *
 * @author 主席大人
 */
@Slf4j
@Service
@Validated
public class HotelRecordServiceImpl implements HotelRecordService {

    private static final String HOTEL_RECORD_TOPIC = "datahub-hotel-record-topic";

    @Resource
    private HotelRecordMapper hotelRecordMapper;
    @Resource
    private MemberUserService memberUserService;

//    @KafkaListener(topics = HOTEL_RECORD_TOPIC, groupId = "warning-gather-group")
    public void receiveDataHubMessage(String message) {
        try {
            // 解析DataHub消息（注意：此处ActiveGather需改为ActiveGather）
            HotelRecordDataHubVO dataHubVO =
                    JSONObject.parseObject(message, HotelRecordDataHubVO.class);
            log.info("接收到DataHub数据: idNumber={}, time={}",
                    dataHubVO.getIdNumber(), dataHubVO.getCheckInTime());
            MemberUserDO userDO = memberUserService.getByIdCard(dataHubVO.getIdNumber());
            if (userDO != null) {
                HotelRecordDO hotelRecordDO = new HotelRecordDO();
                BeanUtils.copyProperties(dataHubVO, hotelRecordDO);
                hotelRecordDO.setPersonId(userDO.getId());
                hotelRecordDO.setCreateTime(LocalDateTime.now());
                hotelRecordMapper.insert(hotelRecordDO);
            }
        } catch (Exception e) {
            log.error("处理DataHub消息异常", e);
        }
    }

    @Override
    public Long createHotelRecord(HotelRecordSaveReqVO createReqVO) {
        // 插入
        HotelRecordDO hotelRecord = BeanUtils.toBean(createReqVO, HotelRecordDO.class);
        hotelRecordMapper.insert(hotelRecord);
        // 返回
        return hotelRecord.getId();
    }

    @Override
    public void updateHotelRecord(HotelRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateHotelRecordExists(updateReqVO.getId());
        // 更新
        HotelRecordDO updateObj = BeanUtils.toBean(updateReqVO, HotelRecordDO.class);
        hotelRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteHotelRecord(Long id) {
        // 校验存在
        validateHotelRecordExists(id);
        // 删除
        hotelRecordMapper.deleteById(id);
    }

    private void validateHotelRecordExists(Long id) {
        if (hotelRecordMapper.selectById(id) == null) {
            throw exception(HOTEL_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public HotelRecordDO getHotelRecord(Long id) {
        return hotelRecordMapper.selectById(id);
    }

    @Override
    public PageResult<HotelRecordDO> getHotelRecordPage(HotelRecordPageReqVO pageReqVO) {
        return hotelRecordMapper.selectPage(pageReqVO);
    }

}